package com.xs.tool;

import lombok.experimental.UtilityClass;

import java.io.*;

@UtilityClass
public class CloneUtils {

    public static <T extends Serializable> T deepClone(T object) {
        try {
            // Write to bytes
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(object);
            oos.flush();
            oos.close();

            // Read from bytes
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);

            return (T) ois.readObject();
        } catch (Exception e) {
            throw new RuntimeException("Deep clone failed", e);
        }
    }
}
